/**
 * 敌方坦克
 * 横坐标，纵坐标，方向
 */
class Enemy {
    constructor() {
        // 想得到坐标就必须 先得到宽度，想得到宽度就必须先得到图片
        // 想得到图片就必须知道方向
        let temp = ["up", "down", "left", "right"];
        // 0-3之间的随机数
        this.dir = temp[parseInt(Math.random() * 4)];
        if (this.dir == "up") {
            this.img = imgList[13];
        }
        else if (this.dir == "down") {
            this.img = imgList[14];
        }
        else if (this.dir == "left") {
            this.img = imgList[15];
        }
        else if (this.dir == "right") {
            this.img = imgList[16];
        }
        this.width = this.img.width;
        this.height = this.img.height;
        this.x = parseInt(Math.random() * (game.width - this.width));
        this.y = parseInt(Math.random() * (game.height - this.height));
        //随机产生坦克的速度
        this.speed = parseInt(Math.random() * 3) + 3;
        //出生的图片
        this.bornImgs = imgList.slice(25, 29);
        //是否正在出生
        this.isBorning = true;
        this.bornImgIndex = 0;
    }
    move() {
        //在移动之前，检测一下千分之五概率，去调整方向
        if (parseInt(Math.random() * 1000) < 5) {
            this.adjustDir();
        }

        //在负右正，上负下正
        if (this.dir == "up") {
            this.y -= this.speed;
        }
        else if (this.dir == "down") {
            this.y += this.speed;
        }
        else if (this.dir == "left") {
            this.x -= this.speed;
        }
        else if (this.dir == "right") {
            this.x += this.speed;
        }
        //当坦克走到边界以后（任意的边界点），都要随机调整一个方向

        if (this.x < 0) {
            this.x = 0;
            this.adjustDir();
        }
        else if (this.x > game.width - this.width) {
            this.x = game.width - this.width;
            this.adjustDir();
        }
        if (this.y < 0) {
            this.y = 0;
            this.adjustDir();
        }
        else if (this.y > game.height - this.height) {
            this.y = game.height - this.height;
            this.adjustDir();
        }

        //加一个随机概率 ，发射子弹
        if (parseInt(Math.random() * 1000) < 8) {
            this.shoot();
        }

    }
    /**
     * 绘制自己
     * @param {CanvasRenderingContext2D} ctx 
     */
    draw(ctx) {
        //判断一下，是否正在出生
        if (this.isBorning) {
            ctx.drawImage(this.bornImgs[parseInt(this.bornImgIndex)%4], this.x, this.y, this.width, this.height);
            this.bornImgIndex +=0.2;
            if (this.bornImgIndex >= 16) {
                this.isBorning = false;
            }
        }
        else {
            //每次绘制自己之前，得到自己这个方向的图片
            if (this.dir == "up") {
                this.img = imgList[13];
            }
            else if (this.dir == "down") {
                this.img = imgList[14];
            }
            else if (this.dir == "left") {
                this.img = imgList[15];
            }
            else if (this.dir == "right") {
                this.img = imgList[16];
            }

            //每次绘制自己的时候，让自己移动一下
            this.move();
            ctx.drawImage(this.img, this.x, this.y, this.width, this.height);
        }


    }
    // 随机调整一个方向
    adjustDir() {
        let temp = ["left", "right", "up", "down"];
        let randomDir = null;
        do {
            randomDir = temp[parseInt(Math.random() * 4)];
        } while (randomDir == this.dir);
        this.dir = randomDir;
    }
    //发射子弹的方法
    shoot() {
        // 创建一个敌方坦克的子弹
        let eb = new EnemyBullet(this.x, this.y, this.dir);
        eb.x = this.x + this.width / 2 - eb.width / 2;
        eb.y = this.y + this.height / 2 - eb.height / 2;
        //将子弹放到集合当中去
        enemyBulletList.push(eb);
    }
}
